﻿namespace Dhgms.Whipstaff.Showcase.Desktop
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        public readonly Dictionary<string, ResourceDictionary> Themes;

        /// <summary>
        /// Initializes a new instance of the <see cref="App"/> class.
        /// </summary>
        public App()
        {
            this.Themes = new Dictionary<string, ResourceDictionary>
                         {
                             { "Blues", this.GetResourceDictionary("Blues.xaml") },
                             { "BuGn", this.GetResourceDictionary("BuGn.xaml") },
                             { "BuPu", this.GetResourceDictionary("BuPu.xaml") },
                             { "GnBu", this.GetResourceDictionary("GnBu.xaml") },
                             { "Greens", this.GetResourceDictionary("Greens.xaml") },
                             { "Greys", this.GetResourceDictionary("Greys.xaml") },
                             { "Oranges", this.GetResourceDictionary("Oranges.xaml") },
                             { "OrRd", this.GetResourceDictionary("OrRd.xaml") },
                             { "PuBu", this.GetResourceDictionary("PuBu.xaml") },
                             { "PuBuGn", this.GetResourceDictionary("PuBuGn.xaml") },
                             { "PuRd", this.GetResourceDictionary("PuRd.xaml") },
                             { "Purples", this.GetResourceDictionary("Purples.xaml") },
                             { "RdPu", this.GetResourceDictionary("RdPu.xaml") },
                             { "Reds", this.GetResourceDictionary("Reds.xaml") },
                             { "YlGn", this.GetResourceDictionary("YlGn.xaml") },
                             { "YlGnBu", this.GetResourceDictionary("YlGnBu.xaml") },
                             { "YlOrBr", this.GetResourceDictionary("YlOrBr.xaml") },
                             { "YlOrRd", this.GetResourceDictionary("YlOrRd.xaml") }
            };
        }

        private string _currentTheme;

        public string CurrentTheme
        {
            get { return _currentTheme; }
            set { _currentTheme = value; }
        }

        public void ChangeTheme(string theme)
        {
            if (theme != _currentTheme)
            {
                var item = Themes.First(pair => pair.Key == theme);
                _currentTheme = theme;
                this.Resources.MergedDictionaries.Clear();
                this.Resources.MergedDictionaries.Add(item.Value);
            }
        }

        ResourceDictionary GetResourceDictionary(string source)
        {
            return Application.LoadComponent(new Uri("/Assets/" + source, UriKind.Relative)) as ResourceDictionary;
        }
    }
}
